<!-- 报告公示 -->
<template>
  <div>
    <card>
      <p slot="title">
        <Row>
          <Col span="12" style="line-height:32px !important">报告公示
          </Col>
          <Col span="12" style="text-align: right">
            <Button @click="jumpPublic" type="primary">公示</Button>
          </Col>
        </Row>
      </p>
      <table id="baogaogongshi" width="100%" cellspacing="0" cellpadding="0" border="1" ref="print">
        <tr>
          <th colspan="4">报告公示表</th>
        </tr>
        <tr>
          <td width="25%">项目编号</td>
          <td colspan="3" class="sizeStyle">
            {{info.ProjectNumber}}
          </td>
        </tr>
        <tr>
          <td>项目名称</td>
          <td colspan="3" class="sizeStyle">
            {{info.ProjectName}}
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'projectName')" v-if='projectNameList.length>0'/>
            <Oldname v-if="projectName" :oldName="projectNameList"></Oldname>
            <!-- <Input v-model="info.Project_name" /> -->
          </td>
        </tr>
        <tr>
          <td>建设单位</td>
          <td colspan="3">
            <Input v-model="info.BuildUnit" :disabled='viewOnly'/>
            <Icon type="ios-arrow-down" size='15' style="cursor:pointer" @click="viewClick($event,'buildName')" v-if='buildNameList.length>0'/>
            <Oldname v-if="buildName" :oldName="buildNameList"></Oldname>
          </td>
        </tr>
        <tr>
          <td>建设地点</td>
          <td colspan="3" class="sizeStyle">
            {{info.BuildAddress}}
            <!-- <Input v-model="info.Build_address" /> -->
          </td>
        </tr>
        <tr>
          <td>报告类型</td>
          <td colspan="3" class="sizeStyle">
            {{info.ReportType}}
            <!-- <Input v-model="info.Report_type" /> -->
          </td>
        </tr>
        <tr>
          <td width="25%">项目负责人</td>
          <td width="25%" class="sizeStyle">
            {{PerjectLeader}}
          </td>
          <td width="25%">项目组其他成员</td>
          <td width="25%" class="sizeStyle">
            {{cyz}}
          </td>
        </tr>
        <tr>
          <td width="25%">审批部门</td>
          <td width="25%" class="sizeStyle">
            {{info.ApprovalUnit}}
          </td>
          <td width="25%">公示时间</td>
          <td width="25%">
            <!-- {{this.$moment().format('YYYY-MM-DD')}} -->
            <Input v-model="publicInfo.PublicityTime" placeholder="请输入公示时间"></Input>
          </td>
        </tr>
        <tr>
          <td rowspan="2" width="25%">公式网址</td>
          <td colspan="3" class="left">
            <!-- 网站名：环保公示 -->
            <Input v-model="publicInfo.PublicityUrlName" :isDisabled='viewOnly' placeholder="请输入公示网站名称">
              <span slot="prepend">网站名：</span>
            </Input>
          </td>
        </tr>
        <tr>
          <td colspan="3" class="left" width="75%">
            <Input v-model="publicInfo.PublicityUrl" :isDisabled='viewOnly' placeholder="请输入公示网址">
              <span slot="prepend">公示链接：</span>
            </Input>
          </td>
        </tr>
        <tr>
          <td width="25%">公示截图</td>
          <td colspan="3" width="75%">
            <Col span="12"><UploadFile v-model="info.PublicImg" :upName="PublicImg" UploadFileName="PublicImg" :UploadFileId="$route.query.ProjectId" :isDisabled='viewOnly'/></Col>
            <Col span="12"><DownloadButton v-if="viewOnly" :downData="downloadInfo.PublicImg"></DownloadButton></Col>
          </td>
        </tr>
        <tr>
          <td width="25%">公众参与报告</td>
          <td colspan="3" width="75%">
            <Col span="12"><UploadFile v-model="info.ReportPublic" :upName="ReportPublic" UploadFileName="ReportPublic" :UploadFileId="$route.query.ProjectId" :isDisabled='viewOnly'/></Col>
            <Col span="12"><DownloadButton v-if="viewOnly" :downData="downloadInfo.ReportPublic"></DownloadButton></Col>
          </td>
        </tr>
        <tr>
          <td rowspan="3" width="25%">是否收到投诉及意见</td>
          <td colspan="3" width="75%">
            <RadioGroup v-model="whether" @on-change="btn">
              <Radio label="yes" :disabled='viewOnly'>
                <span>是</span>
              </Radio>
              <Radio label="no" :disabled='viewOnly'>
                <span>否</span>
              </Radio>
            </RadioGroup>
          </td>
        </tr>
        <tr>
          <td colspan="3">
            <!-- 99%改成100% -->
            <div style="width:100%;margin:auto;height:100%;">
              <Table border ref="selection" style="border: none" :columns="publicityQuestionsListOption" :data="publicityQuestionsList"></Table>
            </div>
          </td>
        </tr>
        <tr>
          <td colspan="3">
            <div v-if='!forbid'>
              <Button type="primary" @click="inputAdd('publicityQuestionsList')" :disabled='viewOnly'>添加</Button>
              <Button type="error" @click="inputRe('publicityQuestionsList')" :disabled='viewOnly'>删除</Button>
            </div>
            <div v-if="forbid">
              <Button type="primary" @click="inputAdd('publicityQuestionsList')" :disabled='true'>添加</Button>
              <Button type="error" @click="inputRe('publicityQuestionsList')" :disabled='true'>删除</Button>
            </div>
          </td>
        </tr>
      </table>
      <Row type="flex" justify="space-between" class="topmargin">
        <Col span="4">
        <PrintButton v-if="flag" Url="print/printPublicShow" :Project_id="info.Id"></PrintButton>
        </Col>
        <Col span="4">
        <div style="float: right">
          <Button type="primary" @click="submit('save')" :disabled='viewOnly'>保存</Button>
          <Button type="primary" @click="submit('submit')" :disabled='viewOnly'>提交</Button>
        </div>
        </Col>
      </Row>
      <StepBar :status="8" :viewOnly='viewOnly'/>
    </card>
  </div>
</template>

<script>
  import libs from "../../../libs/uploads";
  export default {
    data() {
      return {
        // 公示时间，网址，名称等
        publicInfo: {
          PublicityTime: '',
          PublicityUrlName: '',
          PublicityUrl: ''
        }, 
        // 意见选否时显示禁选状态
        forbid:false,
        // 项目负责人
        PerjectLeader: '',
        // 项目名称、建设单位、法人、联系人、联系电话留痕列表
        projectName: false,
        buildName: false,
        projectNameList: [], 
        buildNameList: [],  
        viewOnly: false,  // 允许查看
        flag: false,
        info: {},
        cyz: '',
        downloadInfo: {},
        publicityQuestionsList: [],
        publicityQuestionsListOption: [{
            align: "center",
            width: 80,
            title: "序号",
            type: 'index'
          },
          {
            title: '投诉意见',
            key: 'PublicityOpinion',
            render: (h, {
              row,
              index
            }) => {
              return h("Input", {
                props: {
                  type: "textarea",
                  value: row.PublicityOpinion
                },
                on: {
                  "on-blur": val => {
                    this.publicityQuestionsList[index].PublicityOpinion = val.target.value;
                    // this.publicityQuestionsList[index]["Project_id"] = this.$route.query.Project_id ||this.$route.query.id;
                    this.publicityQuestionsList[index]["ProjectId"] = this.$route.query.ProjectId
                  }
                }
              });
            }
          },
          {
            title: '解决结果',
            key: 'ReviseExplain',
            render: (h, {
              row,
              index
            }) => {
              return h("Input", {
                props: {
                  type: "textarea",
                  value: row.ReviseExplain
                },
                on: {
                  "on-blur": val => {
                    this.publicityQuestionsList[index].ReviseExplain = val.target.value;
                    // this.publicityQuestionsList[index]["Project_id"] = this.$route.query.Project_id || this.$route.query.id;
                    this.publicityQuestionsList[index]["ProjectId"] = this.$route.query.ProjectId
                  }
                }
              });
            }
          }

        ],
        whether: '',
        ReportPublic: "",
        PublicImg: "",
        updatedInfo: 0,
        setButton: 0,
      }
    },
    created() {
      let query = this.$route.query
      this.query = query
      // this.getProject(this.query.id)
      //判断是否有ID
      if (this.$route.query.ProjectId && this.$route.query.ProjectId != "") {
        this.getProject(this.$route.query.ProjectId);
        if(this.$route.query.viewOnly==1 || localStorage.getItem('userOrg') ==7) {
          this.viewOnly = true
        }
      } else {
        //跳转首页
        this.$router.push({
          name: "index"
        });
      }
      // 项目成员组
      this.findStaffs()
    },
    watch: {
      // 意见显示状态
      whether() {
        if(this.viewOnly==0) {
          if(this.whether=='no') {
            this.forbid = true
            
          }else {
            this.forbid = false
          }
        }
      }
    },
    methods: {
      // 名字去重显示
      viewClick(e,nameShow) {
        if(!this[nameShow]) {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-up'
        }else {
          e.target.className = 'ivu-icon ivu-icon-ios-arrow-down'
        }
        this[nameShow] = !this[nameShow]
      },
      //点击添加意见
      inputAdd(type) {
        this[type].push({})
      },
      inputRe(type){
        this[type].pop()
      },
      /* 获取当前创建项目111 */
      getProject(Id) {
        this.$post('ProjectInfo.findProjectByID', {
            Id: Id || this.$route.query.ProjectId
        }).then(res => {
          // 公示时间网址等
          if(res.publicityURLlist&&res.publicityURLlist.length>0) {
            this.publicInfo = res.publicityURLlist[0]
          }
          this.PerjectLeader = res.PerjectLeader
          this.downloadInfo = res.doc
          this.info = res.info
          // 项目名字留痕
          if(res.pt2List.length>0) {
            var arrAll = res.pt2List //所有更改记录
            function arrayUnique2(arr, ProjectName) {
              var hash = {};
              return arr.reduce(function (item, next) {
                hash[next[ProjectName]] ? '' : hash[next[ProjectName]] = true && item.push(next);
                return item;
              }, []);
            }
            var projectNameList = arrayUnique2(arrAll, "ProjectName")
            if(projectNameList[0].ProjectName==this.info.ProjectName) {
              projectNameList = arrayUnique2(arrAll, "ProjectName").slice(1)
            }
            projectNameList.forEach(e => {
              this.projectNameList.push({name: e.ProjectName,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });

            var buildNameList = arrayUnique2(arrAll, "BuildUnit")
            if(buildNameList[0].BuildUnit==this.info.BuildUnit) {
              buildNameList = arrayUnique2(arrAll, "BuildUnit").slice(1)
            }
            buildNameList.forEach(e => {
              this.buildNameList.push({name: e.BuildUnit,Operator: e.Operator,UpdateTime: e.UpdateTime})
            });           
          }
          
          this.publicityQuestionsList = res.publicityQuestionsList
          if(this.publicityQuestionsList.length>0) {
            this.whether='yes'
          }else {
            this.whether = 'no'
          }
          localStorage.projectType = this.info.PerjectType
          this.flag = true
          this.PublicImg = this.upWord(res.doc.PublicImg)
          this.ReportPublic = this.upWord(res.doc.ReportPublic)
        }).catch(err => {
          console.log(err)
          this.$Message.error('获取当前项目失败')
          // alert('请求崩溃1231')
        })
      },
      btn(type){
        if(type == 'no'){
          this.$Modal.confirm({
            title: '提示',
            content: '确定未收到投诉及意见，并清空填写内容',
            onOk: ()=>{
              this.publicityQuestionsList = []
            },
            onCancel: ()=>{
              this.whether='yes'
            }
          })
        }
      },
      findStaffs() {
        axios.get('/audit/findStaffs2', {
          params: {
            ProjectId: this.query.Id || this.query.ProjectId
          }
        }).then(res => {
          console.log(res)
          this.cyz = res.data
          // if (res._STATUS == 1) {
          //   let cyz = []
          //   res.data.forEach((e, i) => {
          //     cyz.push(e['Staffs_name'])
          //   });
          //   this.cyz = cyz.join(',')
          // }
        })
      },
      publicityQuestionsListFn() {
        let publicityQuestionsList = this.publicityQuestionsList
        console.log(publicityQuestionsList)
        axios.post('/publicityQuestions/saveOrUpdate', {
          PublicityQuestionsList:JSON.stringify(publicityQuestionsList)
        }).then(res => {
          // this.$Message.success('添加成功')
          console.log(res)
        }).catch(err => {
          console.log(err)
          // alert('提交失败')
        })
      },
      // 提交公示时间，地址，名称等
      publicInfoSubmit() {
        axios.post('/publicityURL/saveOrUpdate',{
          ProjectId:this.$route.query.ProjectId,
          PublicityTime: this.publicInfo.PublicityTime,
          PublicityUrlName: this.publicInfo.PublicityUrlName,
          PublicityUrl: this.publicInfo.PublicityUrl
        }).then(res=>{
          // console.log(res)
        })
      },
      // 保存 save / 提交 submit
      submit(type) {
        let info = this.info
        let appraiseListXg = this.publicityQuestionsList
        info.BtnWay = type == 'save' ? 1 : 2
        info.PerjectType = type == 'save' ? 14 : 14
        this.publicityQuestionsListFn()
        this.publicInfoSubmit() //提111交公示时间网址等

        if(type == 'save'){
          if(this.setButton == 0){
            for(var keys=0;keys<this.publicityQuestionsList.length;keys++){
              let nRongs = this.publicityQuestionsList[keys].ReviseExplain;
              let nRong = this.publicityQuestionsList[keys].PublicityOpinion;
              
              if((nRong==""||nRong==null)&&(nRongs==""||nRongs==null)){
                // console.log(nRong);
                this.publicityQuestionsList.splice(keys,1);
              }
              
            }
           
            this.setButton = 1
            setTimeout(()=>{this.setButton = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                  this.$router.push({
                    name: "reportArchives",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {this.$Message.error('请求失败')})
          }else{
            this.$Message.warning('连续点击保存时间过短，最少间隔1分钟!')
          }
        }else{
          if(this.updatedInfo == 0){
            this.updatedInfo = 1
            setTimeout(()=>{this.updatedInfo = 0},60000)
            axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
              if (res._STATUS == 1) {
                let title = res._MESSAGE
                if (type == 'save') {
                  this.$Modal.success({
                    title,
                  })
                } else {
                  this.$router.push({
                    name: "reportArchives",
                    query: {
                      ProjectId: this.$route.query.ProjectId
                    }
                  })
                }
              } else {
                this.$Modal.error({title:res._MESSAGE})
              }
            }).catch(err => {this.$Message.error('请求失败')})
          }else{
            this.$Message.warning('连续点击提交时间过短，最少间隔1分钟!')
          }
        }

      //   if(this.setButton == 0){
      //     this.setButton = 1
      //     setTimeout(()=>{this.setButton = 0},60000)
        
      //   let info = this.info
      //   info.btn_way = type == 'save' ? 1 : 2
      //   info.Perject_type = type == 'save' ? 14 : 14
      //   // if(this.updatedInfo <= 2){
      //   //   this.$Message.warning('未修改数据无法提交')
      //   //   return
      //   // }
      //   this.publicityQuestionsListFn()
      //   this.publicInfoSubmit() //提交公示时间网址等
      //   axios.post('/projectInformation/saveOrUpdateProject', info).then((res) => {
      //     if (res._STATUS == 1) {
      //       let title = res._MESSAGE
      //       if (type == 'save') {
      //         this.$Modal.success({
      //           title,
      //         })
      //       } else {
      //         this.$router.push({
      //           name: "reportArchives",
      //           query: {
      //             // id: this.$route.query.id || this.$route.query.Project_id
      //             Project_id: this.$route.query.Project_id
      //           }
      //         })
      //       }
      //     } else {
      //       // 提示
      //       this.$Modal.error({
      //         title:res._MESSAGE
      //       })
      //     }
      //   }).catch(err => {
      //     console.log(err)
      //     this.$Message.error('请求失败')
      //   })
      // }else{
      //   this.$Message.warning('请勿重复提交!')
      // }
      },
      jumpPublic() {            //跳转公式
        var uId = localStorage.getItem('uId')
        console.log(uId)
        var ProjectId = this.$route.query.ProjectId
        window.open(libs.uploads3() + 'home/PublicEditor?ProjectId='+ProjectId+ '&uId=' + uId)
        // window.open('http://localhost:8080/home/PublicEditor?Project_id='+Project_id)
      },
      upWord(url){        //分割字符串
        if(url == null){
          return '上传'
        }else{
          var upWord = url;
          upWord = upWord.split("/");
          upWord = upWord.slice(-1);
          upWord = upWord.join('');
          // console.log(upWord)
          return upWord
        }
      },
    },
  }
</script>

<style lang="less">
  #baogaogongshi {
    td.sizeStyle {
      font-size:12px;
    }
    textarea.ivu-input {
      font-size: 12px;
    }
    .ivu-input {
      border: none !important;
      text-align: center;
    }
    .ivu-input[disabled], fieldset[disabled] .ivu-input {
      background-color: #fff; 
      color: #333;
      font-size: 12px;
    }
  }
</style>